package com.andreas.serviceImp;

import com.andreas.mapper.BookMapper;
import com.andreas.pojo.Book;
import com.andreas.service.BookService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @Author andreaszhou
 * @ClassName BookServiceImp
 * @Description TODO
 * @date 2021/1/19 19:48
 * @Version 1.0
 */
@Service
@Transactional(propagation = Propagation.NOT_SUPPORTED,readOnly = true)
public class BookServiceImp implements BookService {
    @Resource
    private BookMapper bookMapper;
    @Override
    public IPage<Book> showBookService(Integer categoryId, String order,Integer page, Integer rows) {
        Page<Book> iPage = new Page<Book>(page,rows);
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        if (categoryId!= null && categoryId != -1){
            queryWrapper.eq("category_id",categoryId);
        }
        if (order!=null){
            if (order.equals("quantity")){
                queryWrapper.orderByDesc("evaluation_quantity");
            }else if (order.equals("score")){
                queryWrapper.orderByDesc("evaluation_score");
            }
        }
        Page<Book> bookPage = bookMapper.selectPage(iPage, queryWrapper);
        return bookPage;
    }

    @Override
    @Transactional
    public void updateEvaluationService() {
        bookMapper.updateEvaluation();
    }

    @Override
    @Transactional
    public Book createBook(Book book) {
        bookMapper.insert(book);
        return book;
    }
}
